<?php
namespace backend\models;

use Yii;
use yii\base\Model;
use yii\base\Exception;
use common\models\CheckUser;

class DataForm extends Model
{
	public $barcode;
	public $name;
	public $sex;
	public $mobileNumber;
	public $check_time;

	public function formName()
    {
        return '';
    }

	public function rules()
	{
		return [
			[['barcode', 'name', 'sex', 'mobileNumber', 'check_time'], 'safe']
		];
	}

	public function query()
	{
		$data = CheckUser::find()->where('id <> 0');
		if ($this->barcode) {
			$data = $data->andWhere(['barcode' => $this->barcode]);
		} 
		if ($this->name) {
			$data = $data->andWhere(['name' => $this->name]);
		}
		if ($this->sex) {
			$data = $data->andWhere(['gender' => $this->sex]);
		}
		if ($this->mobileNumber) {
			$data = $data->andWhere(['mobileNumber' => $this->mobileNumber]);
		}
		if ($this->check_time) {
			$re = explode('-', $this->check_time);
			$begin = strtotime(trim($re[0]));
			$end = strtotime(trim($re[1]));
			$data = $data->andWhere('samplingDate >= ' . $begin)->andWhere('samplingDate <= ' . $end);
		}

		return $data;
	}

}	